/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ServerConfig.java
 *
 * Created on Aug 26, 2010, 3:20:56 AM
 */
package GUI_Config;

import DA.*;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

/**
 *
 * @author thangvv0907i
 */
public class ServerConfig extends javax.swing.JFrame {

    /** Creates new form ServerConfig */
    private Connecting frmConnect;

    public ServerConfig() {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        Dimension ds = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation((ds.width - this.getWidth()) / 5, (ds.height - this.getHeight()) / 5);
        initComponents();
        setSize(460, 190);
        setResizable(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel6 = new javax.swing.JLabel();
        txtServer = new javax.swing.JTextField();
        txtPort = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtPassword = new javax.swing.JPasswordField();
        jLabel4 = new javax.swing.JLabel();
        chkExample = new javax.swing.JCheckBox();
        txtDatabase = new javax.swing.JTextField();
        btnInstall = new javax.swing.JButton();
        btnExit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Install");
        getContentPane().setLayout(null);

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel6.setText("Inventory Management Server Config");
        getContentPane().add(jLabel6);
        jLabel6.setBounds(80, 10, 270, 20);
        getContentPane().add(txtServer);
        txtServer.setBounds(300, 40, 130, 20);

        txtPort.setText("1433");
        getContentPane().add(txtPort);
        txtPort.setBounds(90, 100, 60, 20);

        jLabel5.setText("Port:");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(20, 100, 24, 14);

        jLabel1.setText("Servername:");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(230, 40, 62, 20);

        jLabel2.setText("Username:");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(20, 40, 52, 14);
        getContentPane().add(txtUsername);
        txtUsername.setBounds(90, 40, 120, 20);

        jLabel3.setText("Password:");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(20, 70, 50, 14);
        getContentPane().add(txtPassword);
        txtPassword.setBounds(90, 70, 120, 20);

        jLabel4.setText("Database:");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(230, 70, 50, 14);

        chkExample.setSelected(true);
        chkExample.setText("Install example records");
        getContentPane().add(chkExample);
        chkExample.setBounds(20, 130, 137, 23);

        txtDatabase.setText("Inventory");
        getContentPane().add(txtDatabase);
        txtDatabase.setBounds(300, 70, 130, 20);

        btnInstall.setText("Install >");
        btnInstall.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInstallActionPerformed(evt);
            }
        });
        getContentPane().add(btnInstall);
        btnInstall.setBounds(300, 130, 73, 23);

        btnExit.setText("Exit");
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });
        getContentPane().add(btnExit);
        btnExit.setBounds(380, 130, 51, 23);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnInstallActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInstallActionPerformed
        if (!validateForm()) {
            return;
        }
        frmConnect = new Connecting();
        frmConnect.setLocation(this.getX() + this.getWidth() / 2 - frmConnect.getWidth() / 2, this.getY() + this.getHeight() / 2 - frmConnect.getHeight() / 2);
        frmConnect.setVisible(true);

        ConnectDB tc = new ConnectDB(this, txtServer.getText(), txtPort.getText(), txtUsername.getText(), String.valueOf(txtPassword.getPassword()), txtDatabase.getText());
        tc.start();
}//GEN-LAST:event_btnInstallActionPerformed

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
        this.dispose();
}//GEN-LAST:event_btnExitActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ServerConfig().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnInstall;
    private javax.swing.JCheckBox chkExample;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField txtDatabase;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtPort;
    private javax.swing.JTextField txtServer;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables

    //MeThod validate ServerConfig Form
    private boolean validateForm() {
        char[] pas = txtPassword.getPassword();
        String pass = new String(pas);
        if (txtUsername.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Must Enter UserName");
            txtUsername.requestFocus();
            return false;
        } else if (pass.equals("")) {
            JOptionPane.showMessageDialog(this, "Must Enter Password");
            txtPassword.requestFocus();
            return false;
        } else if (txtServer.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Must Enter ServerName");
            txtServer.requestFocus();
            return false;
        }
        return true;
    }

    //Method Install DB
    public void action(int i) {
        frmConnect.dispose();
        if (i == 0) {
            Install install = new Install(chkExample.isSelected());
            this.dispose();
            install.setLocation(this.getX() + this.getWidth() / 5 - install.getWidth() / 5, this.getY() + this.getHeight() / 5 - install.getHeight() / 5);
            install.setVisible(true);
        } else {
            JOptionPane.showMessageDialog(this, "Cannot open this database");
        }
    }
}

class ConnectDB extends Thread {

    private ServerConfig frmParent;
    private String sServerName;
    private String sPort;
    private String sUsername;
    private String sPassword;
    private String sDatabase;

    public ConnectDB(ServerConfig frmParent, String serverName, String port, String username, String password, String database) {
        this.frmParent = frmParent;
        sServerName = serverName;
        sPort = port;
        sUsername = username;
        sPassword = password;
        sDatabase = database;
    }

    @Override
    public void run() {
        if (ConnectionDB.connect(sServerName, sPort, sUsername, sPassword, sDatabase)) {
            frmParent.action(0);
        } else {
            frmParent.action(1);
        }
    }
}
